package com.jpress.model;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;

import com.jpress.model.Usuario;

@Entity
@Table(name="comentario")
@NamedQueries({
	@NamedQuery(name="Comentario.countComentarios", query="SELECT COUNT(c) FROM Comentario c"),
	@NamedQuery(name="Comentario.findAllByUsuarioCodigo", query="SELECT c FROM Comentario c WHERE c.usuario.codigo = :usuarioCodigo ORDER BY c.codigo DESC"),
	@NamedQuery(name="Comentario.findAllByNoticiaCodigo", query="SELECT c FROM Comentario c WHERE c.noticia.codigo = :noticiaCodigo ORDER BY c.codigo DESC")
})
public class Comentario implements Serializable {

	private static final long serialVersionUID = 8034769912129559991L;

	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	@Column(name="codigo")
	private int codigo;
	
	@ManyToOne
	private Usuario usuario; //autor do comentario
	
	@ManyToOne
	private Noticia noticia;
	
	@Column(name="texto", nullable=false, length=1000)
	private String texto;
	
	@Column(name="ativo")
	private boolean ativo;
	
	@Temporal(TemporalType.TIMESTAMP)
	@Column(name="dataCadastro", nullable=false)
	private Date dataCadastro;
	
	public Comentario(){}

	public int getCodigo() {
		return codigo;
	}

	public void setCodigo(int codigo) {
		this.codigo = codigo;
	}

	public Usuario getAutor() {
		return usuario;
	}

	public void setUsuario(Usuario usuario) {
		this.usuario = usuario;
	}

	public Noticia getNoticia() {
		return noticia;
	}

	public void setNoticia(Noticia noticia) {
		this.noticia = noticia;
	}

	public String getTexto() {
		return texto;
	}

	public void setTexto(String texto) {
		this.texto = texto;
	}

	public boolean isAtivo() {
		return ativo;
	}

	public void setAtivo(boolean ativo) {
		this.ativo = ativo;
	}

	public Date getDataCadastro() {
		return dataCadastro;
	}

	public void setDataCadastro(Date dataCadastro) {
		this.dataCadastro = dataCadastro;
	}
	
	@Override
	public boolean equals(Object object){
		return EqualsBuilder.reflectionEquals(this, object);
	}

	@Override
	public int hashCode() {
		return HashCodeBuilder.reflectionHashCode(this);
	}		
}
